package com.prd.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.api.RemoteUserService;
import com.prd.config.LoginInterceptor;
import com.prd.dto.BudgetApproveDto;
import com.prd.dto.DeviationDto;
import com.prd.mapper.EntMeetSummaryBudgetMapper;
import com.prd.model.*;
import com.prd.service.EntMeetSummaryApproveService;
import com.prd.service.EntMeetSummaryBudgetService;
import com.prd.service.EntMeetSummaryService;
import com.prd.util.StringsUtils;
import com.prd.util.UserInfoUtil;
import com.prd.vo.BudgetVo;
import com.prd.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class EntMeetSummaryBudgetServiceImpl extends ServiceImpl<EntMeetSummaryBudgetMapper, EntMeetSummaryBudget> implements EntMeetSummaryBudgetService {


    @Autowired
    private EntMeetSummaryBudgetMapper entMeetSummaryBudgetMapper;

    @Autowired
    private EntMeetSummaryService entMeetSummaryService;

    @Autowired
    private EntMeetSummaryApproveService entMeetSummaryApproveService;

    @Autowired
    private RemoteUserService remoteUserService;

    @Override
    public int insertInfo(EntMeetSummaryBudget entMeetSummaryMotion) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entMeetSummaryMotion);
        return entMeetSummaryBudgetMapper.insert(entMeetSummaryMotion);
    }

    @Override
    public int updateInfo(EntMeetSummaryBudget entMeetSummaryMotion) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entMeetSummaryMotion);
        return entMeetSummaryBudgetMapper.updateById(entMeetSummaryMotion);
    }

    @Override
    public int addBudgetApprove(BudgetApproveDto budgetApproveDto) {
        EntMeetSummary entMeetSummary = entMeetSummaryService.getById(budgetApproveDto.getId());
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(budgetApproveDto, entMeetSummary);
        return entMeetSummaryService.updateInfo(entMeetSummary);
    }

    @Override
    public int addDeviation(DeviationDto deviationDto) {
        EntMeetSummary entMeetSummary = entMeetSummaryService.getById(deviationDto.getId());
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(deviationDto, entMeetSummary);
        entMeetSummary.setDeviationTime(new Date());
        return entMeetSummaryService.updateInfo(entMeetSummary);
    }

    @Override
    public JSONArray jurisdictionBudget(Long summaryId) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        EntMeetSummary entMeetSummary = entMeetSummaryService.getById(summaryId);
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        JSONArray jsonArray = new JSONArray();
        if (entMeetSummary.getBudgetApplication().equals(loginUser.getId().toString())) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", "budgetApplication");
            jsonObject.put("name", "预算申请人");
            jsonArray.add(jsonObject);
        }
        if (entMeetSummary.getBudgetCouncilor().equals(loginUser.getId().toString())) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", "budgetCouncilor");
            jsonObject.put("name", "预/决算督导");
            jsonArray.add(jsonObject);
        }
        if (entMeetSummary.getBudgetApprove().equals(loginUser.getId().toString())) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", "budgetApprove");
            jsonObject.put("name", "预算审批人");
            jsonArray.add(jsonObject);
        }
        if (entMeetSummary.getBudgetMake().equals(loginUser.getId().toString())) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", "budgetMake");
            jsonObject.put("name", "预算抄送人");
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    @Override
    public BudgetVo listBudget(Long summaryId) {
        EntMeetSummary entMeetSummary = entMeetSummaryService.getById(summaryId);
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BudgetVo budgetVo = new BudgetVo();
        BeanUtils.copyProperties(entMeetSummary, budgetVo);

        List<Long> arrayList = new ArrayList<>();
        Long budgetApplication = Long.parseLong(StringUtils.isBlank(entMeetSummary.getBudgetApplication()) ? "0" : entMeetSummary.getBudgetApplication());
        Long budgetCouncilor = Long.parseLong(StringUtils.isBlank(entMeetSummary.getBudgetCouncilor()) ? "0" : entMeetSummary.getBudgetCouncilor());
        Long budgetApprove = Long.parseLong(StringUtils.isBlank(entMeetSummary.getBudgetApprove()) ? "0" : entMeetSummary.getBudgetApprove());
        Long budgetMake = Long.parseLong(StringUtils.isBlank(entMeetSummary.getBudgetMake()) ? "0" : entMeetSummary.getBudgetMake());
        arrayList.add(budgetApplication);
        arrayList.add(budgetCouncilor);
        arrayList.add(budgetApprove);
        arrayList.add(budgetMake);

        List<Long> deviationReports = Arrays.asList(StringsUtils.convert(entMeetSummary.getDeviationReport().split(",")));
        List<Long> deviationJointlys = Arrays.asList(StringsUtils.convert(entMeetSummary.getDeviationJointly().split(",")));
        arrayList.addAll(deviationReports);
        arrayList.addAll(deviationJointlys);
        List<UserVo> listUser = remoteUserService.listInfo(arrayList.toArray(new Long[arrayList.size()])).getData();

        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        for (UserVo userVo : listUser) {
            if (budgetApplication == userVo.getId()) {
                budgetVo.setBudgetApplication(userVo.getName() + " " + userVo.getTelephone());
            }
            if (budgetCouncilor == userVo.getId()) {
                budgetVo.setBudgetCouncilor(userVo.getName() + " " + userVo.getTelephone());
            }
            if (budgetApprove == userVo.getId()) {
                budgetVo.setBudgetApprove(userVo.getName() + " " + userVo.getTelephone());
            }
            if (budgetMake == userVo.getId()) {
                budgetVo.setBudgetMake(userVo.getName() + " " + userVo.getTelephone());
            }

            if (deviationReports.contains(userVo.getId())) {
                list1.add(userVo.getName());
            }
            if (deviationJointlys.contains(userVo.getId())) {
                list2.add(userVo.getName());
            }
        }
        budgetVo.setDeviationReport(String.join(" ", list1));
        budgetVo.setDeviationJointly(String.join(" ", list2));

        //预算
        LambdaQueryWrapper<EntMeetSummaryBudget> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EntMeetSummaryBudget::getSummaryId, summaryId);
        List<EntMeetSummaryBudget> listEntMeetSummaryBudget = entMeetSummaryBudgetMapper.selectList(lambdaQueryWrapper);
        for (EntMeetSummaryBudget budget : listEntMeetSummaryBudget) {
            if (budget.getType() == 1) {
                budgetVo.setBudget(budget);
            }
            if (budget.getType() == 2) {
                budgetVo.setPractical(budget);
            }
            if (budget.getType() == 3) {
                budgetVo.setDifference(budget);
            }
            if (budget.getType() == 4) {
                budgetVo.setAddBudget(budget);
            }
        }
        //预算审批
        LambdaQueryWrapper<EntMeetSummaryApprove> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntMeetSummaryApprove::getSummaryId, summaryId);
        List<EntMeetSummaryApprove> listEntMeetSummaryApprove = entMeetSummaryApproveService.list(queryWrapper);
        for (EntMeetSummaryApprove approve : listEntMeetSummaryApprove) {
            if (approve.getType() == 1) {
                budgetVo.setOpinion(approve);
            }
            if (approve.getType() == 2) {
                budgetVo.setPass(approve);
            }
            if (approve.getType() == 3) {
                budgetVo.setFail(approve);
            }
        }

        return budgetVo;
    }

    @Override
    public int addApprove(EntMeetSummaryApprove entMeetSummaryApprove) {
        EntMeetSummary entMeetSummary = entMeetSummaryService.getById(entMeetSummaryApprove.getSummaryId());
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        LambdaQueryWrapper<EntMeetSummaryApprove> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EntMeetSummaryApprove::getType, entMeetSummaryApprove.getType());
        List<EntMeetSummaryApprove> entMeetSummaryApproveList = entMeetSummaryApproveService.list(lambdaQueryWrapper);
        if (entMeetSummaryApproveList != null && entMeetSummaryApproveList.size() > 0) {
            throw new RuntimeException("你已提交过了！");
        }
        if (entMeetSummaryApprove.getType() == 2 || entMeetSummaryApprove.getType() == 3) {
            entMeetSummary.setApproveStatus(entMeetSummaryApprove.getType());
            entMeetSummaryService.updateInfo(entMeetSummary);
        }
        return entMeetSummaryApproveService.insertInfo(entMeetSummaryApprove);
    }
}
